<?php
/**
 * 通用页面
 *
 *
 *
 * @copyright  Copyright (c) 2010-2015 NcPHP Inc. (http://www.ncphp.com)
 * @license    http://www.ncphp.com
 * @link       http://www.ncphp.com
 * @since      File available since Release v1.1
 */
defined('InNcPHP') or exit('Access Invalid!');

class commonControl extends SystemControl{
	public function __construct(){
		parent::__construct();
	}

	// 图片上传
	public function pic_uploadOp(){
		if (chksubmit()){
			//上传图片
			$upload = new UploadFile();
			$upload->set('thumb_width',	500);
			$upload->set('thumb_height',499);
			$upload->set('thumb_ext','_small');
			$upload->set('max_size',C('image_max_filesize')?C('image_max_filesize'):1024);
			$upload->set('ifremove',true);
			$upload->set('default_dir',$_GET['uploadpath']);
			if (!empty($_FILES['_pic']['tmp_name'])){
				$result = $upload->upfile('_pic');
				if ($result){
					exit(json_encode(array('status'=>1,'url'=>UPLOAD_SITE_URL.'/'.$_GET['uploadpath'].'/'.$upload->thumb_image)));
				}else {
					exit(json_encode(array('status'=>0,'msg'=>$upload->error)));
				}
			}
		}
	}

	// 图片裁剪
	public function pic_cutOp(){
		Language::read('admin_common');
		$lang = Language::getLangContent();
		import('function.thumb');
		if (chksubmit()){
			$thumb_width = $_POST['x'];
			$x1 = $_POST["x1"];
			$y1 = $_POST["y1"];
			$x2 = $_POST["x2"];
			$y2 = $_POST["y2"];
			$w = $_POST["w"];
			$h = $_POST["h"];
			$scale = $thumb_width/$w;
			$src = str_ireplace(UPLOAD_SITE_URL,BASE_UPLOAD_PATH,$_POST['url']);
			if (!empty($_POST['filename'])){
				$save_file2 = str_ireplace(UPLOAD_SITE_URL,BASE_UPLOAD_PATH,$_POST['filename']);
			}else{
				$save_file2 = str_replace('_small.','_sm.',$src);
			}
			$cropped = resize_thumb($save_file2, $src,$w,$h,$x1,$y1,$scale);
			@unlink($src);
			$pathinfo = pathinfo($save_file2);
			exit($pathinfo['basename']);		
		}
		$save_file = str_ireplace(UPLOAD_SITE_URL,BASE_UPLOAD_PATH,$_GET['url']);
		$_GET['resize'] = $_GET['resize'] == '0' ? '0' : '1';
		Tpl::output('height',get_height($save_file));
		Tpl::output('width',get_width($save_file));
		Tpl::showpage('common.pic_cut','null_layout');
	}
	
	/**
	 * 查询每月的周数组
	 */
	public function getweekofmonthOp(){
	    import('function.datehelper');
	    $year = $_GET['y'];
	    $month = $_GET['m'];
	    $week_arr = getMonthWeekArr($year, $month);
	    echo json_encode($week_arr);
	    die;
	}

	// 地图坐标获取
	public function getAmapPoiOp(){
		$area = trim($_GET['area']);
		Tpl::showpage('common.amap','null_layout');
	}

	public function getAreaOp(){
		$area_list = Model('area')->getAreaList(array('area_pid'=>trim($_GET['area_pid'])));
		echo json_encode($area_list);
	}

	public function getVillageOp(){
		$village_list = Model('village')->getVillageList(array('area_id'=>trim($_GET['area_id'])));
		echo json_encode($village_list);
	}

    /**
     * 获取子级的地区
     */
    public function getAreaChildOp(){
        $parent_id = $_GET['parent_id'];
        /* @var areaModel $area_model */
        $area_model = Model('area');
        $areaList = $area_model->getAreaList(array('parent_id'=>$parent_id));
		die(json_encode($areaList));
    }

	/**
	 * 获取指定商家底下权限组
	 */
	public function getMerchant_groupOp()
	{
		$mc_id = intval($_GET['mc_id']);
		$model = Model('admin_group');
		if($mc_id>0){
			$group_list = $model->getAdminGroupList(array('g_merchant_id'=>$mc_id),'group_id,group_name',100);
		}else{
			$group_list = array();
		}
		exit(json_encode($group_list));
	}

    /**
     * 获取分类
     */
    public function getCategoryOp(){
        $cate_list = Model('ticket_category')->getCategoryList(array('tc_pid'=>trim($_GET['tc_pid']),'is_del'=>0));
        echo json_encode($cate_list);
    }

	/**
	 * 获取地理坐标
	 */
	public function bdmapOp(){
		$model = Model('area');
		$cityId = trim($_GET['cityId']);
		$areaId = trim($_GET['areaId']);
		$proviceId = trim($_GET['proviceId']);//省ID
		$store_address= trim($_GET['store_address']);
		$proviceId = $model->getfby_id($proviceId,'name');
		$cityName = $model->getfby_id($cityId,'name');
		$areaName = $model->getfby_id($areaId,'name');
		Tpl::output('region', $proviceId.$cityName.$areaName);
		Tpl::output('store_address', $store_address);
		Tpl::setDir('');
		Tpl::showpage('gdmap', 'null_layout');
	}


	/**
	 * ajax删除照片
	 */
	public function delimgOp()
	{
		$img_id = intval($_POST['img_id']);
		/* @var ticket_imagesModel $images_model */
		$images_model = Model('spec_images');
		$res = $images_model->delSpecImage(array('img_id'=>$img_id));
		$this->log('删除图片成功'.'[img_id:'.$img_id.']', $res?1:0);
		exit(json_encode(array('res'=>$res)));
	}

	/**
	 * ajax删除照片
	 */
	public function all_delimgOp()
	{
		$img_id = intval($_POST['img_id']);
		/* @var ticket_imagesModel $images_model */
		$images_model = Model('ticket_images');
		$res = $images_model->delTicketImage(array('img_id'=>$img_id));
		$this->log(L('nc_del,ticket_index_photo').'[img_id:'.$img_id.']', $res?1:0);
		exit(json_encode(array('res'=>$res)));
	}
}